package com.octant.testipc;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.tuya.smart.home.sdk.TuyaHomeSdk;
import com.tuya.smart.sdk.api.IResultCallback;
import com.tuya.smart.sdk.api.ITuyaDevice;
import com.tuya.smart.sdk.bean.DeviceBean;

import java.util.Map;

public class DpUtil {

    /**
     * 1	正常
     * 2	异常（ SD 卡损坏或格式不对）
     * 3	空间不足
     * 4	正在格式化
     * 5	无 SDK 卡
     */
    public static final String DP_ID_SDCARD_STATUS = "110";
    public static final String DP_ID_SDCARD_FORMAT_COMMAND = "111";
    public static final String DP_ID_SDCARD_FORMAT_STATUS = "117";
    //总容量|已使用容量|空闲容量，单位`KB`
    public static final String DP_ID_SDCARD_STORAGE_SPACE = "109";
    public static final String DP_ID_SDCARD_RECORD_SWITCH = "150";
    /**
     * 1	事件录像（检测到移动再录像到 SD 卡）
     * 2	连续录像
     */
    public static final String DP_ID_SDCARD_RECORD_MODE = "151";


    public static Object queryValueByDPID(String devId,String dpId) {
        DeviceBean deviceBean = TuyaHomeSdk.getDataInstance().getDeviceBean(devId);
        if (deviceBean != null) {
            Map<String, Object> dps = deviceBean.getDps();
            if (dps != null) {
                return dps.get(dpId);
            }
        }
        return null;
    }

    public static Object queryValueByKey(String mapStr, String keyStr) {
        if (mapStr != null) {
            Map<String, Object> dps = (Map) JSONObject.parseObject(mapStr, Map.class);
            if (dps.containsKey(keyStr)) {
                return dps.get(keyStr);
            }
        }
        return null;
    }

    public static void publishDps(Context context,ITuyaDevice iTuyaDevice, String dpId, Object value) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(dpId, value);
        String dps = jsonObject.toString();
        iTuyaDevice.publishDps(dps, new IResultCallback() {
            @Override
            public void onError(String code, String error) {
                Toast.makeText(context,"发送命令失败,"+error,Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onSuccess() {
                //Toast.makeText(context,"发送命令成功",Toast.LENGTH_SHORT).show();
            }
        });
    }
}
